package gitee.derumin.really_forge.enchantments;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import gitee.derumin.really_forge.items.HammerItem;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.ItemStack;

import java.util.Map;

public class HeavinessEnchantment extends Enchantment implements CanChangeAttributeModifier {
    public HeavinessEnchantment(Rarity weight, EnchantmentTarget target, EquipmentSlot[] slotTypes) {
        super(weight, target, slotTypes);
    }

    @Override
    public Multimap<EntityAttribute, EntityAttributeModifier> changeAttributeModifiers(Multimap<EntityAttribute, EntityAttributeModifier> target, int level) {
        if (target.isEmpty()){
            return target;
        }
        ImmutableMultimap.Builder<EntityAttribute, EntityAttributeModifier> builder = ImmutableMultimap.builder();
        for (Map.Entry<EntityAttribute, EntityAttributeModifier> entry : target.entries()) {
            if (entry.getKey().equals(EntityAttributes.GENERIC_ATTACK_DAMAGE) && entry.getValue().getOperation().equals(EntityAttributeModifier.Operation.ADDITION)){
                EntityAttributeModifier modifier = entry.getValue();
                builder.put(EntityAttributes.GENERIC_ATTACK_DAMAGE, new EntityAttributeModifier(modifier.getId(), modifier.getName(), (modifier.getValue()+1)*(1+level*0.1)-1, modifier.getOperation()));
            }else {
                builder.put(entry.getKey(),entry.getValue());
            }
        }
        return builder.build();
    }

    @Override
    public int getMaxLevel() {
        return 5;
    }

    @Override
    public int getMinPower(int level){
        return 5 + ( level - 1) * 10;
    }

    @Override
    public int getMaxPower(int level) {
        return this.getMinPower(level) + 15;
    }

    @Override
    protected boolean canAccept(Enchantment other) {
        return super.canAccept(other) && !(other instanceof LitheEnchantment);
    }

    @Override
    public boolean isAcceptableItem(ItemStack stack) {
        return stack.getItem() instanceof HammerItem;
    }

    @Override
    public void onTargetDamaged(LivingEntity user, Entity target, int level) {
        if (target instanceof LivingEntity livingEntity) {
            if (user.getRandom().nextFloat() < level * 0.05) {
                int i = 60 + user.getRandom().nextInt(20 * level);
                livingEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, i, 1));
            }
        }

    }
}
